import { ExpeditionOrderDishManage } from './../../components/expeditionOrderDishManage';
import { AppModel } from 'app/models/kdsConfig';
import { ConfigSvr } from './../../services/config';
import { NodeService } from './../../services/system';
import { Subscription } from 'rxjs';
import { MessageBoxService, GrowlMessageType } from './../../components/messageBox';
import { MessageHandleManager } from './../../utils/messageHandle/messageHandleManager';
import { FNCManager, KdsMessageType } from './../../utils/fnc/fncManager';
import { StoreSvr } from './../../services/store';
import { AppStore, System } from './../../services/appStore';
import { NavigateService } from './../../services/navigate';
import { Component, Directive, ElementRef, OnInit, style } from '@angular/core';




interface tip {
    iconClass: string;
    warningText: string;
    btnText: string;
}


@Component({
    styleUrls: ['./initialization.scss'],
    template: ` 
        <div class="checkoldenv-container">
            <div *ngIf="!currentFaildNodeSvr" class="divtcvma" style="text-align:center; color: black;">
                        <i  class=" icon-loading iconfont icon-refresh "></i>
                        <p style="padding:30px;margin:0;font-size: 16px;">加载中</p>
            </div>
            <div *ngIf="currentFaildNodeSvr" class="container">
                <img  src="./assets/img/kds.png" class="divtcvma" style="transform: translate(-50%, -50%) scale(0.75);top:45%;"/>
                <div  class="failed divtcvma">
                    <p class="title">{{currentFaildNodeSvr&&currentFaildNodeSvr.describe}}--未开启</p>
                    <p class="content">{{currentFaildNodeSvr&&currentFaildNodeSvr.errorTip}}</p>
                    <button  (click)="reCheck()" >重新检测</button>
                </div>
            </div>
        </div>
    `,
    providers: []
})

export class CheckOldEnv {
    private subscribetion: Subscription;
    private tips: tip[];
    private currentFaildNodeSvr: NodeService;
    constructor(
        private appStore: AppStore,
        private navSvr: NavigateService,
        private storeSvr: StoreSvr,
        private configSvr: ConfigSvr
    ) {

    }

    private async ngOnInit() {
        await this.appStore.systemStore.actions.setAdsAbout();
        this.checkEnv();
    }
    private async checkEnv() {
        try {
            await this.appStore.systemStore.actions.checkLDCOnce()
            await this.appStore.systemStore.actions.checkFbsOnce()
            //没有错误
            // 链接fns
            this.appStore.systemStore.actions.fncConnect();
            this.appStore.systemStore.actions.doBackgroundTask();

            this.subscribetion = this.appStore.systemStore.systemObservable.subscribe(system => {
                //如果上次是成功的，下次失败了就提示？？？ todo
                system.nodeServices && system.nodeServices.forEach(x => {
                    if (x.status == "failed") {
                        MessageBoxService.current.nodeServiceWarn(x);
                    } else {
                        MessageBoxService.current.nodeServiceWarn(null);
                    }
                })
            });
            //更新device
            await this.storeSvr.refreshDeviceForIp();
            //更新kitchensolutions
            await this.storeSvr.refreshLocalsotrage();
            await MessageBoxService.current.refreshKitchenSolutionName();
            await ExpeditionOrderDishManage.current.resetExpeditionOrder();
            switch (this.configSvr.appConfig.model) {
                case AppModel.FastFood:
                    this.navSvr.navigate(["home"]);
                    break;
                case AppModel.HotPot:
                    this.navSvr.navigate(["newHome"]);
                    break;
                default:
                    break;
            }
        } catch (error) {
            this.currentFaildNodeSvr = error;
        }
    }
    private ngOnDestroy() {

    }
    private reCheck() {
        this.checkEnv()
    }
}
